<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        li {
            list-style: none;
        }

        a {
            color: inherit;
            text-decoration: none;
        }

        img {
            vertical-align: top;
        }

        .w {
            width: 1226px;
            margin: 0 auto;
        }

        .cl::after {
            content: "";
            display: block;
            clear: both;
            height: 0;
            overflow: hidden;
            visibility: hidden;
        }

        .main {
            margin-top: 50px;
        }

        .main .preview-wrap {
            float: left;
            width: 400px;
            margin-right: 10px;
            min-height: 600px;
            /* background-color: grey; */
            position: relative;
        }

        .main .itemInfo-wrap {
            overflow: hidden;
            min-height: 600px;
            /* background-color: pink; */
        }

        .preview {
            width: 400px;
            height: 400px;
            position: relative;
        }

        .preview .smallImg {
            width: 100%;
            height: 100%;
        }

        .preview .shadow {
            width: 200px;
            height: 200px;
            background-color: rgba(0, 0, 0, 0.4);
            position: absolute;
            left: 0;
            top: 0;
            display: none;
        }

        .preview-scale {
            width: 400px;
            height: 400px;
            background-color: rgba(125, 255, 255, 0.5);
            overflow: hidden;
            position: absolute;
            left: 410px;
            top: 0;
            display: none;
        }

        .preview-scale img {
            width: 800px;
            height: 800px;
            position: absolute;
            left: 0;
            top: 0;
            /* z-index: -1; */
        }

        .list>p {
            width: 54px;
            height: 54px;
            border: 1px solid #333;
            margin-right: 10px;
        }

        .list>p.active {
            border-color: red;
            float: left;
        }
    </style>
</head>

<body>
    <div class="container main w cl">
        <div class="preview-wrap">
            <div class="preview">
                <img src="./images/1.big.jpg" alt="" class="smallImg">
                <div class="shadow"></div>
            </div>
            <!-- <div class="spec-list"></div> -->
            <div class="preview-scale">
                <img src="./images/1.big.jpg" alt="" class="bigImg">
            </div>
        </div>
        <div class="itemInfo-wrap"></div>
    </div>
    <div class="list">
        <p class="active">
            <img src="./images/1.small.jpg" showImg="./images/1.big.jpg" enlargeImg="./images/1.big.jpg" alt="">
        </p>
        <p>
            <img src="./images/2.small.jpg" showImg="./images/2.big.jpg" enlargeImg="./images/2.big.jpg" alt="">
        </p>
    </div>
</body>
<script>
    var previewWrap = document.getElementsByClassName("preview-wrap")[0];
    var preview = document.getElementsByClassName("preview")[0];
    var previewScale = document.getElementsByClassName("preview-scale")[0];

    var shadow = document.getElementsByClassName("shadow")[0];

    var smallImg = document.getElementsByClassName("smallImg")[0];
    var bigImg = document.getElementsByClassName("bigImg")[0];




    preview.onmouseenter = function () {
        previewScale.style.display = "block";
        shadow.style.display = "block";
    }
    preview.onmouseleave = function () {
        previewScale.style.display = "none";
        shadow.style.display = "none";
    }

    preview.onmousemove = function (e) {
        var e = e || window.event;
        var x = e.pageX - previewWrap.offsetLeft - shadow.clientWidth / 2;
        var y = e.pageY - previewWrap.offsetTop - shadow.clientHeight / 2;

        var maxLeft = preview.clientWidth - shadow.clientWidth;
        var maxTop = preview.clientHeight - shadow.clientHeight;

        // 计算缩放比例 
        var scale = bigImg.clientWidth / smallImg.clientWidth;
        console.log(scale, bigImg.clientWidth, smallImg.clientWidth);

        if (x <= 0) x = 0;
        if (x >= maxLeft) x = maxLeft;
        if (y <= 0) y = 0;
        if (y >= maxTop) y = maxTop;

        // 取值时记得去单位  赋值时记得加单位
        shadow.style.left = x + "px";
        shadow.style.top = y + "px";

        bigImg.style.left = -(scale * x) + "px";
        bigImg.style.top = -(scale * y) + "px";
    }



</script>

</html>